--[[
Ammo Gnome, looks after your ammo.

Todo:
Add support for buying specific ammo
Add support for none quivers (?)
Add debugging
Remove dongle (?)
]]

--[[			=Locals=			]]
local class = select(2,UnitClass("player"))
local Current,Total
local projectile = Arrow, Bullet

--[[			=Init=			]]
local AmmoGnome = DongleStub("Dongle-1.1"):New("AmmoGnome")

--[[			=db INIT=			]]
local databasedefaults = {
	profile = {	["Bag Overide"] = false,
			["Bag"] = 4,
			["Ammo Overide"] = false,
			["Ammo"] = "Razor Arrow",
		},
}

function AmmoGnome:Initialize()
	local name = UnitName("Player")
	self.db = self:InitializeDB("AmmoGnomeDB", databasedefaults)
	if class == "HUNTER" then else 	--Hunters only ATM
		self:Disable()
		self:Print("You disguise does'nt fool Ammo Gnome"..class)
		end
	if name == "Kanne" then
		self:EnableDebug(5)
    end
end

function AmmoGnome:Enable()
	self:RegisterEvent("MERCHANT_SHOW", "AmmoBag")
end

function AmmoGnome:Disable()
	self:Print("Disabled")
	self:UnregisterAllEvents()
end

--Finds a quiver/ammo pouch and calculates the total/current amount of projectiles in it.
function AmmoGnome:AmmoBag()
	if self.db.profile["Bag Overide"] == false then
		for bags = 1,4 do
		local BagName = GetBagName(bags)		
		local subtype = select(7, GetItemInfo(BagName))
		self:Debug(1, subtype)
			if subtype == "Quiver" or "Ammo Pouch" then
				for Slot = 0,GetContainerNumSlots(bags) do
				local ItemLink = GetContainerItemLink(bags, Slot)
					if ItemLink then
					local ItemSub = select(6, GetItemInfo(ItemLink))
					Current = GetItemCount(ItemLink)
					Total = (GetContainerNumSlots(bags) * 200)
						if subtype == "Quiver" then
							if ItemSub == "Arrow" or "Bullet" then
								self:BuyAmmo(ItemLink)
								self:Debug(1, itemlink)
							end
						end
				break
					end
				end
			end
		end
	else
		self:SpecBag(self.db.profile["Bag"])
    end
end

--[[	Specifiy your own bag to use with what ammo	]]
function AmmoGnome:SpecBag(bag)
	if self.db.profile["Ammo Overide"] == false then
	self:Debug(1, bag)
		--Find ammo in said bag
		for slot =0,GetContainerNumSlots(bag) do
		local ItemLink = GetContainerItemLink(bags, Slot)
			if ItemLink then
			local ItemSub = select(6, GetItemInfo(ItemLink))
				if ItemSub == "Arrow" or "Bullet" then
					Current = GetItemCount(ItemLink)
					Total = (GetContainerNumSlots(bags) * 200)
					self:BuyAmmo(ItemLink)
				end
			end
		end
	end
end

--[[	Some function to buy specific ammo, may have to change the BuyAmmo()?	]]
function AmmoGnome:SpecifAmmo()
	if self.db.profile["Ammo Overide"] == true then
		self:BuyAmmo(self.db.profile["Ammo"])
		self:Debug(1, self.db.profile["Ammo"])
	end
end

function AmmoGnome:SortAmmo(b)
	local bags = {}
	for s = 0,GetContainerNumSlots(b) do
		local ItemLink = GetContainerItemLink(b, s)
		if not ItemLink then
			bag[s] = ItemLink
		end
	end
	for k,v in pairs(bags) do
		self:BuyAmmo(v)
		bags[s] = nil
	end
end

function AmmoGnome:BuyAmmo(ItemLink)
	for MerchID = 0,GetMerchantNumItems() do
		local ItemLinkMerch = GetMerchantItemLink(MerchID)
		if (ItemLinkMerch == ItemLink) then
			BuyMerchantItem(MerchID)
		end
	end
end

function AmmoGnome:F()

end